﻿Shader "Level4/Nature/Snow" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_BumpMap("Bump Map",2d) = ""{}
		_Snow("Snow Level",float) = 0.1
		_SnowColor("Snow Color",color) = (1,1,1,1)
		_SnowDirection("Snow Direction",Vector) = (1,1,0,0)
		_SnowDepth("Snow Depth",float) = 0.1
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert //vertex:vert

		sampler2D _MainTex;
		sampler2D _BumpMap;
		float _Snow;
		fixed4 _SnowColor;
		fixed4 _SnowDirection;
		float _SnowDepth;

		struct Input {
			float2 uv_MainTex;
			float2 uv_BumpMap;
			float3 worldNormal;
			INTERNAL_DATA
		};
		
		void vert(inout appdata_full v){
			float4 sn = mul(UNITY_MATRIX_IT_MV,_SnowDirection);
			if(dot(v.normal,sn.xyz) >= lerp(1,-1,(_Snow*2)/3)){
				v.vertex.xyz += (sn.xyz + v.normal) * _SnowDepth * _Snow;
			}
		}

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			half3 n = UnpackNormal(tex2D(_BumpMap,IN.uv_BumpMap));
			
			o.Albedo = c;
			if(dot(WorldNormalVector(IN,n),_SnowDirection.xyz) > lerp(1,-1,_Snow))
				o.Albedo = _SnowColor;
			
			o.Alpha = c.a;
			o.Normal = n;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}
